package com.hzb.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.hzb.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.hzb.gulimall.product.entity.AttrEntity;
import com.hzb.gulimall.product.service.AttrAttrgroupRelationService;
import com.hzb.gulimall.product.service.AttrService;
import com.hzb.gulimall.product.service.CategoryService;
import com.hzb.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hzb.gulimall.product.entity.AttrGroupEntity;
import com.hzb.gulimall.product.service.AttrGroupService;
import com.hzb.common.utils.PageUtils;
import com.hzb.common.utils.R;



/**
 * 
 *
 * @author huangZB
 * @email 576566606@qq.com
 * @date 2020-08-26 23:22:10
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService attrgroupRelationService;

    /**
     * /product/attrgroup/{catelogId}/withattr
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId")Long catelogId){

        List<AttrGroupWithAttrsVo> vos = attrGroupService.getgetAttrGroupWithAttrsBycCtelogId(catelogId);
        return R.ok().put("data",vos);
    }


    /**
     * /product/attrgroup/attr/relation
     * @return
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrAttrgroupRelationEntity> attrgroupRelationEntity){

        attrgroupRelationService.addRelation(attrgroupRelationEntity);
        return R.ok();
    }
    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId){
        //PageUtils page = attrGroupService.queryPage(params);

        PageUtils page =attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }

    /**
     * 获取属性分组的关联的所有属性
     * @param attrgroupId
     * @return
     */
    @GetMapping("{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){

        List<AttrEntity> attrEntities = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",attrEntities);
    }



    /**
     * 获取属性分组没有关联的其他属性
     * @param attrgroupId
     * @param params
     * @return
     */
    @GetMapping("{attrgroupId}/noattr/relation")
    public R getNoRelationAttr(@PathVariable("attrgroupId") long attrgroupId,
                               @RequestParam Map<String,Object> params){

        PageUtils page = attrService.getNoRelationAttr(attrgroupId,params);
        return R.ok().put("page",page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long[] catelogPath = categoryService.findCatelogPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 删除 /product/attrgroup/attr/relation/delete
     */
    @RequestMapping("/attr/relation/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R deleteRelation(@RequestBody List<AttrAttrgroupRelationEntity> attrgroupRelationEntities){
        //attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        attrService.deleteRelation(attrgroupRelationEntities);
        return R.ok();
    }

}
